﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QBS.Tools
{
    public class SqlTransaction
    {
        public SqlTransaction()
        {
            sqlTable = new List<Dictionary<string, object>>();
        }
        private List<Dictionary<string, object>> sqlTable;
        public void Start()
        {
            sqlTable.Clear();
        }
        public int Commit()
        {
            int flag = DbHelper.ExecuteSqlTran(sqlTable);
            sqlTable.Clear();
            return flag;
        }
        public virtual int Count()
        {
            return sqlTable.Count();
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public virtual void Delete(int id, int branch_id = 0)
        {
            string sql = string.Format("delete from [{0}] where id ={1} {2}", this.GetType().Name, id, (branch_id > 0 ? "and branch_id=" + branch_id : ""));
            Add(sql);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public virtual void Delete(string where)
        {
            string sql = string.Format("delete from [{0}] ", this.GetType().Name);
            if (!string.IsNullOrEmpty(where))
                sql += " where " + where;
            Add(sql);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public virtual void Add(string sql, object param = null)
        {
            Dictionary<string, object> dc = new Dictionary<string, object>();
            dc.Add("sql", sql);
            dc.Add("params", param);
            sqlTable.Add(dc);
        }
    }
}